package com.openxt.nbzj.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.openxt.nbzj.R;
import com.openxt.nbzj.adapter.ViewPagerAdapter;
import com.openxt.nbzj.fragment.NoticeFragment;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.fragment.OpenxtFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 颖杰_2 on 2016-07-27.
 */
public class NoticeActivity extends OpenxtBaseActivity implements TabLayout.OnTabSelectedListener {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    List<OpenxtFragment> mList = new ArrayList<>();
    ViewPagerAdapter viewPagerAdapter = null;
    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.notice_activity, "通知公告");
        ButterKnife.bind(this);

        setActionUrl("#/noticelist");

        NoticeFragment f = new NoticeFragment();
        f.setName("最新公告");
        f.setType(0);
        mList.add(f);
        f = new NoticeFragment();
        f.setName("公告收藏");
        f.setType(1);
        mList.add(f);

        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.setList(mList);
        viewPager.setOffscreenPageLimit(mList.size());
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(0);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mList.get(0).refresh();
            }
        }, 0);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        final int position = tabLayout.getSelectedTabPosition();
        viewPager.setCurrentItem(position);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mList.get(position).refresh();
            }
        }, 0);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
